//
//  DateRangeFormCell.swift
//  Employee
//
//  Created by swordjoy on 2017/6/26.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class DateRangeFormCell: UITableViewCell {
  
  static let identifier = "DateRangeFormCell.identifier"
  static let height: CGFloat = 150
  
  var isDetail: Bool = false {
    didSet {
      if isDetail {
        startButton.isUserInteractionEnabled = false
        endButton.isUserInteractionEnabled = false
        titleLabel.textColor = GlobalConfig.TextLightColor
        startButton.setTitleColor(GlobalConfig.TextDeepColor, for: .normal)
        endButton.setTitleColor(GlobalConfig.TextDeepColor, for: .normal)
      }
    }
  }
  
  var model: FormItem? {
    didSet {
      guard let temp = model else { return }
      if temp.required {
        titleLabel.text = temp.name
      }
      else {
        titleLabel.text = temp.name + "(选填)"
      }
    }
  }
  var detailModel: InspectionProject? {
    didSet {
      guard let temp = detailModel else { return }
      titleLabel.text = temp.name
      if let startTemp = temp.value.first, !startTemp.isEmpty { startButton.setTitle(startTemp, for: .normal) }
      else {
        startButton.setTitle("未选择", for: .normal)
        startButton.setTitleColor(GlobalConfig.TextLightColor, for: .normal)
      }
      if let endTemp = temp.value.last, !endTemp.isEmpty { endButton.setTitle(endTemp, for: .normal) }
      else {
        endButton.setTitle("未选择", for: .normal)
        endButton.setTitleColor(GlobalConfig.TextLightColor, for: .normal)
      }
    }
  }
  
  
  var start: String = "" {
    didSet {
      if start.isEmpty {
        startButton.setTitle("点击选择开始时间", for: .normal)
      }
      else { startButton.setTitle(start, for: .normal) }
    }
  }
  
  var end: String = "" {
    didSet {
      if start.isEmpty {
        endButton.setTitle("点击选择结束时间", for: .normal)
      }
      else { endButton.setTitle(end, for: .normal) }
    }
  }
  
  var getTime: ( (String, String, String) -> () )?
  
  fileprivate var titleLabel: UILabel!
  fileprivate var startButton: UIButton!
  fileprivate var endButton: UIButton!
  fileprivate var topCircle: UIView!
  fileprivate var bottomCircle: UIView!
  fileprivate var verticalLine: UIView!
  
  // MARK: - life cycle
  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    
    create()
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}

// MARK: - action

extension DateRangeFormCell {
  
  @objc fileprivate func startAction() {
    let dateV = SJDatePickerView(style: SJDatePickerStyle())
    dateV.getDatePickerDate = { dateString in
        self.start = dateString
        self.getTime?(dateString, self.end, self.model!.name)
    }
    dateV.show()
  }
  
  @objc fileprivate func endAction() {
    let dateV = SJDatePickerView(style: SJDatePickerStyle())
    dateV.getDatePickerDate = { dateString in
        self.end = dateString
        self.getTime?(self.start, dateString, self.model!.name)
    }
    dateV.show()
  }
  
}

// MARK: - fileprivate

extension DateRangeFormCell {
  
  fileprivate func create() {
    selectionStyle = .none
    backgroundColor = UIColor.white
    
    titleLabel = UILabel.getLabel(with: GlobalConfig.system15Font, textColor: GlobalConfig.TextDeepColor)
    contentView.addSubview(titleLabel)
    titleLabel.snp.makeConstraints { (make) in
      make.left.equalTo(20)
      make.top.equalTo(25)
    }
    
    verticalLine = UIView()
    contentView.addSubview(verticalLine)
    verticalLine.backgroundColor = GlobalConfig.BGWhiteColor
    verticalLine.snp.makeConstraints { (make) in
      make.left.equalTo(50)
      make.width.equalTo(3)
      make.top.equalTo(70)
      make.bottom.equalTo(-30)
    }
    
    topCircle = UIView()
    contentView.addSubview(topCircle)
    topCircle.backgroundColor = GlobalConfig.BGWhiteColor
    topCircle.snp.makeConstraints { (make) in
      make.centerX.equalTo(verticalLine)
      make.top.equalTo(verticalLine.snp.top).offset(5)
      make.width.height.equalTo(10)
    }
    topCircle.layer.cornerRadius = 5
    topCircle.clipsToBounds = true
    
    bottomCircle = UIView()
    contentView.addSubview(bottomCircle)
    bottomCircle.backgroundColor = GlobalConfig.BGWhiteColor
    bottomCircle.snp.makeConstraints { (make) in
      make.centerX.equalTo(verticalLine)
      make.bottom.equalTo(verticalLine.snp.bottom).offset(-5)
      make.width.height.equalTo(10)
    }
    bottomCircle.layer.cornerRadius = 5
    bottomCircle.clipsToBounds = true
    
    startButton = UIButton()
    contentView.addSubview(startButton)
    startButton.titleLabel?.font = GlobalConfig.system14Font
    startButton.contentHorizontalAlignment = .left
    startButton.setTitleColor(GlobalConfig.Text08DeepColor, for: .normal)
    startButton.setTitle("点击选择开始时间", for: .normal)
    startButton.addTarget(self, action: #selector(startAction), for: .touchUpInside)
    startButton.snp.makeConstraints { (make) in
      make.left.equalTo(topCircle.snp.right).offset(12)
      make.centerY.equalTo(topCircle.snp.centerY)
      make.height.equalTo(44)
      make.right.equalTo(-40)
    }
    
    endButton = UIButton()
    contentView.addSubview(endButton)
    endButton.titleLabel?.font = GlobalConfig.system14Font
    endButton.contentHorizontalAlignment = .left
    endButton.setTitleColor(GlobalConfig.Text08DeepColor, for: .normal)
    endButton.setTitle("点击选择结束时间", for: .normal)
    endButton.addTarget(self, action: #selector(endAction), for: .touchUpInside)
    endButton.snp.makeConstraints { (make) in
      make.left.equalTo(topCircle.snp.right).offset(12)
      make.centerY.equalTo(bottomCircle.snp.centerY)
      make.height.equalTo(44)
      make.right.equalTo(-40)
    }
    
  }
  
}
